package com.xuan.common.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xuan.common.core.constants.Constants;
import com.xuan.common.core.utils.ThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 *  必须将 Bean 暴露给外部容器管理,才能自动填充字段
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始插入填充...");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        // TODO 这个数据需要是操作用户的实际ID
        this.strictInsertFill(metaObject, "createBy", Long.class, ThreadLocalUtils.get(Constants.USER_ID, Long.class));
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新填充...");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        // TODO 这个数据需要是操作用户的实际ID
        this.strictUpdateFill(metaObject, "updateBy", Long.class, ThreadLocalUtils.get(Constants.USER_ID, Long.class));
    }


}